import { ADD, UPDATE } from './constants'

const initState = {
  list: [
    {
      id: 1,
      todoName: '呵呵',
      isDone: false,
    },
    {
      id: 2,
      todoName: '嘿嘿',
      isDone: true,
    },
  ],
}
export default function (state = initState, action) {
  let newList
  switch (action.type) {
    case ADD:
      newList = [...state.list]
      newList.push({
        id: Date.now(),
        todoName: action.todoName,
        isDone: false,
      })
      return {
        ...state,
        list: newList,
      }
    case UPDATE:
      newList = state.list.map((item) => {
        if (item.id === action.id) {
          item.isDone = !item.isDone
        }
        return { ...item }
      })
      return {
        ...state,
        list: newList,
      }

    default:
      return state
  }
}
